import hashlib
import struct
import sys


# 调用方式:
# python3 sitem_shard.py [pItemId]
# example:
# python3 sitem_shard.py 12
def get_shard_index(p_item_id):
    num_shards = 128  # 128个分表
    hasher = hashlib.md5()
    hasher.update(struct.pack('>q', p_item_id))  # 使用大端字节序
    hash_bytes = hasher.digest()
    shard_index = int.from_bytes(hash_bytes[:4], byteorder='big') % num_shards
    shard_index = str(shard_index)
    if len(shard_index) == 2:
        shard_index = "0" + shard_index
    elif len(shard_index) == 1:
        shard_index = "00" + shard_index
    return shard_index

if __name__ == '__main__':
    print(get_shard_index(5816))